﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GBMS.MODEL;
using Telerik.Web.UI;
using System.Data;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.APP_UI.Common;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGMKPIResultList_POP : System.Web.UI.Page
    {
        private BGMKPIResultListBLL cmd = new BGMKPIResultListBLL();
        Main main = new Main();
        private SsMng creatby = new SsMng();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int intKpiSeq = Convert.ToInt32(Request.QueryString["Item"]);
                string selectTarget = Request.QueryString["Target"];
                LoadKPIType(intKpiSeq);
                LoadTarget(selectTarget);
                LoadProjectDetail(intKpiSeq);
                LoadKPIName(intKpiSeq);
                LoadGrid(intKpiSeq);
            }
        }

        private void LoadTarget(string selectTarget)
        {                        
            DataTable dt = cmd.ListTargetKPI();
            cboRadTarget.DataSource = dt;
            cboRadTarget.DataBind();
            cboRadTarget.SelectedValue = selectTarget;             
            cboRadTarget.Enabled = false;
            hdfTargetSelect.Value = cboRadTarget.SelectedValue;
        }

        private void LoadKPIType(int intKpiSeq)
        {
            DataTable dt = cmd.GetProject(intKpiSeq);
            object KPIType = dt.Rows[0][4];
            string sKPIType = KPIType.ToString();
            
            DataTable dt1 = cmd.GetKPIListType();
            cboRadKPIType.DataSource = dt1;
            cboRadKPIType.DataBind();

            cboRadKPIType.SelectedValue = sKPIType;
        }

        private void LoadKPIName(int intKpiSeq)
        {
            DataTable dt = cmd.GetProject(intKpiSeq);
            object KPITypeSeq = dt.Rows[0][13];
            int intKPITypeSeq = Convert.ToInt32(KPITypeSeq);
            int intStratListSeq = Convert.ToInt32(dt.Rows[0][1]);

            DataTable dtGetName = cmd.GetName(intKpiSeq, intStratListSeq, intKPITypeSeq);
            object Name = dtGetName.Rows[0][5];
            lblName.Text = Name == DBNull.Value ? "" : Name.ToString();
        }

        private void LoadGrid(int intKpiSeq)
        {
            var data = cmd.SearchTarget(intKpiSeq);
            RadGrid1.DataSource = data;
            RadGrid1.DataBind();
        }

        private void LoadProjectDetail(int intKpiSeq)
        {
            DataTable dt = cmd.GetProject(intKpiSeq);

            object KPIName = dt.Rows[0][2];
            txtRadKPIName.Text = KPIName == DBNull.Value ? "" : KPIName.ToString();

            object KPIDesc = dt.Rows[0][3];
            txtRadDesc.Text = KPIDesc == DBNull.Value ? "" : KPIDesc.ToString();

            object KPIUnit = dt.Rows[0][6];
            txtRadKPIUnit.Text = KPIUnit == DBNull.Value ? "" : KPIUnit.ToString();

            object KPIWeight = dt.Rows[0][7];
            txtRadWeight.Text = KPIWeight == DBNull.Value ? "" : KPIWeight.ToString();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int intKpiSeq = Convert.ToInt32(Request.QueryString["Item"]);
            DataTable dt = cmd.GetProject(intKpiSeq);

            string strKpiName = txtRadKPIName.Text;
            string strDescription = txtRadDesc.Text;
            int intKpiTypeSeq = Convert.ToInt32(cboRadKPIType.SelectedValue);
            string strKpiUnit = txtRadKPIUnit.Text;
            Nullable<int> weight;

            if (string.IsNullOrEmpty(txtRadWeight.Text)) weight = null;
            else weight = Convert.ToInt32(txtRadWeight.Text);

            cmd.KPIUpdate(intKpiSeq, strKpiName, strDescription, intKpiTypeSeq, strKpiUnit, weight);

            //var score = new Dictionary<int, double?>();
            int strKpiDetSeq;
            foreach (GridDataItem item in RadGrid1.Items)
            {
                strKpiDetSeq = Convert.ToInt32(item.GetDataKeyValue("kpi_det_seq"));
                var dblRusult = (RadNumericTextBox)item["Result"].FindControl("txtRadResult");
                cmd.UpdateTarget(strKpiDetSeq, main.ConvertNullDouble(dblRusult.Text), creatby.EmpSeq);
            }

           Page.ClientScript.RegisterStartupScript(GetType(), "Close", "CloseRadWin();", true);
        }


        
    }
}